package com.huawang.mdesign.model.service.model.base;

import com.huawang.business.dto.graph.ModelInfo;
import com.huawang.business.service.model.base.ElementModelInfoService;
import com.huawang.business.service.model.base.ModelManagerConfig;
import com.huawang.core.definition.mof.UMLDefinitionConstants;
import com.huawang.mdesign.model.service.constant.SpringBeanNameConstants;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

@Component(SpringBeanNameConstants.ELEMENT_MODEL_INFO_SERVICE)
@Primary
public class UAFElementModelInfoService extends ElementModelInfoService {
    //匹配数字和字母
    private static final String MATCH_REG = "([0-9]+[a-zA-Z]+|[a-zA-Z]+[0-9]+)[0-9a-zA-Z]*";

    @Autowired
    private ModelManagerConfig managerConfig;

    /**
     * 根据stereotype的id获取modelInfo关键字
     *
     * @param key
     * @return
     */
    @Override
    @NotNull
    public String getModelInfoKeyByStereotypeId(String key) {
        if (key.contains(".Stereotype")) {
            return ".Stereotype";
        }
        if(UMLDefinitionConstants.UAF_System.equals(key) || UMLDefinitionConstants.UAF_Project.equals(key)) {
            return key;
        }
        if (key.contains(".")) {
            key = key.substring(key.lastIndexOf(".") + 1);
        }
        if (key.contains("::")) {
            key = key.substring(key.lastIndexOf("::") + 2);
        }
        return key;
    }

    @Override
    public @Nullable ModelInfo fetchStereotypeModelInfo(@NotNull String id) {
        //排除csm转换过来的应用构造型id
        if (id.contains(".") && !id.split("\\.")[0].matches(MATCH_REG)) {
            id = id.substring(id.lastIndexOf(".") + 1);
        }
        if (UMLDefinitionConstants.UAF_System.equals(id) || UMLDefinitionConstants.UAF_Project.equals(id)) {
            return managerConfig.getModelInfoConfig().get(id);
        }
        if(id.contains("::")) {
            id = id.substring(id.lastIndexOf("::") + 2);
        }
        return managerConfig.getModelInfoConfig().get(id);
    }
}
